package testdatagenerator;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class EditFrame extends JFrame {
	private static final long serialVersionUID = 1L;
	Selector<String> Nodes;
	JCheckBox selectNodes;
	JTextField Mean, stdDev, Freq, amountToSelect;
	Attack toBeEdited;
	JButton Okay;
	public EditFrame(Attack s, GeneralData theData){
		this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		if (theData==null){
			return;
		}
		Vector<String> vec = new Vector<String>();
		for(int i=0;i<theData.getNodes();i++){
			vec.add("Node"+((Integer)i).toString());
		}
		setLayout(new GridLayout(2, 1));
		toBeEdited = s;
		selectNodes = new JCheckBox();
		amountToSelect = new JTextField();
		amountToSelect.setEnabled(false);
		amountToSelect.setText("1");
		Freq = new JTextField();
		Mean = new JTextField();
		stdDev = new JTextField();
		Okay = new JButton("Okay");
		Mean.setPreferredSize(new Dimension(50, 20));
		stdDev.setPreferredSize(new Dimension(50, 20));
		JPanel panel = new JPanel(), panel1 = new JPanel(), panel2 = new JPanel();
		Nodes = new Selector<String>(vec);
		JLabel MeanL = new JLabel("Mean");
		JLabel amountToSelectL = new JLabel("How many nodes:");
		JLabel StdDevL = new JLabel("Std dev:");
		JLabel FreqL = new JLabel("Packet generation frequency:");
		JLabel selectNodesT = new JLabel("Select random connected nodes:");
		ActionListener selectNodesL = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				Nodes.setEnabled(!selectNodes.isSelected());
				amountToSelect.setEnabled(selectNodes.isSelected());
			}
		};
		selectNodes.addActionListener(selectNodesL);
		Mean.setText("3");
		stdDev.setText(".1");
		Freq.setText("1");
		Freq.setPreferredSize(new Dimension(50, 20));
		amountToSelect.setPreferredSize(new Dimension(50, 20));
		panel.setLayout(new GridLayout(2, 1));
		panel2.add(selectNodesT);
		panel2.add(selectNodes);
		panel2.add(amountToSelectL);
		panel2.add(amountToSelect);
		panel1.add(FreqL);
		panel1.add(Freq);
		panel1.add(MeanL);
		panel1.add(Mean);
		panel1.add(StdDevL);
		panel1.add(stdDev);
		panel2.add(Okay);
		panel.add(panel1);
		panel.add(panel2);
		add(Nodes);
		add(panel);
		setResizable(false);
		setSize(625, 450);
		if (s.getMean()!=-1){
			Mean.setText(((Double)s.getMean()).toString());
			stdDev.setText(((Double)s.getStdDev()).toString());
			Freq.setText(((Double)s.getFreq()).toString());
			selectNodes.setSelected(s.getRandConnectedNodes());
			Nodes.setEnabled(!selectNodes.isSelected());
			amountToSelect.setEnabled(selectNodes.isSelected());
			amountToSelect.setText(((Integer)s.getSize()).toString());
			Vector<Integer> vec1 = s.getMemberIds();
			Vector<String> temp = new Vector<String>();
			for(int i=0;i<vec1.size();i++){
				temp.add("Node"+((Integer) vec1.get(i)));
			}
			Nodes.pumpStringEqu(temp);
		}
		ActionListener OkayL = new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				try {
					double mean = Double.parseDouble(Mean.getText()), StdDev = Double.parseDouble(stdDev.getText()), freq = Double.parseDouble(Freq.getText());
					int size = Integer.parseInt(amountToSelect.getText());
					if (mean < 0 || StdDev < 0 || freq < 0 || size < 0){
						throw new Exception();
					}
					toBeEdited.setMean(mean);
					toBeEdited.setStdDev(StdDev);
					toBeEdited.setFreq(freq);
					toBeEdited.setRandConnectedNodes(selectNodes.isSelected());
					toBeEdited.setSize(size);
					String str = "";
					Vector<String> selected = Nodes.getSelectedItems();
					toBeEdited.clearAttacks();
					for(int i=0;i<selected.size();i++){
						str = selected.get(i);
						toBeEdited.addAttack(Integer.parseInt(str.substring(4)));
					}
					//dispose();
                                        
				} catch(Exception exc){
					JOptionPane.showMessageDialog(null, "Please use valid nonnegative doubles, and nonnegative ints where appropriate, please.");
				}
			}
		};
		Okay.addActionListener(OkayL);
	}
}
